#!/usr/bin/env python
#!coding:utf-8

from rest_framework import  serializers
from .models import Product,Host,Api,ApiRunRecord,RunApiPermod,Suite,SuiteArgument,ApiArgument,SuiteRunRecord,SuiteApiRunRecord

class HostSerializer(serializers.ModelSerializer):
	product_id=serializers.IntegerField(write_only=True)
	class Meta:
		model=Host
		fields=['id','name','host','description','product_id']

class ApiArgumentSerializer(serializers.ModelSerializer):
	'''API测试参数'''
	class Meta:
		model=ApiArgument
		fields='__all__'

class ApiSerializer(serializers.ModelSerializer):
	product_id=serializers.IntegerField(write_only=True)
	host=HostSerializer(read_only=True)
	host_id=serializers.IntegerField(write_only=True)
	arguments=ApiArgumentSerializer(many=True,read_only=True)
	class Meta:
		model=Api
		fields='__all__'

class SuiteArgumentSerializer(serializers.ModelSerializer):
	'''全局参数序列化'''
	class Meta:
		model=SuiteArgument
		#把字段suite除外
		exclude=['suite']

class SuiteSerializer(serializers.ModelSerializer):
	'''测试套件'''
	product_id=serializers.IntegerField(write_only=True)
	api_list=ApiSerializer(many=True,read_only=True)
	arguments=SuiteArgumentSerializer(many=True,read_only=True)
	class Meta:
		model=Suite
		exclude=['product']

class ProductSerializer(serializers.ModelSerializer):
	id=serializers.IntegerField(read_only=True)
	last_update_time=serializers.DateTimeField(read_only=True)
	#返回host的信息
	host_list=HostSerializer(many=True,read_only=True)
	#返回测试用例的信息
	api_list=ApiSerializer(many=True,read_only=True)
	#返回产品所属的测试套件
	suite_list=SuiteSerializer(many=True,read_only=True)
	class Meta:
		model=Product
		fields=['id','name','product_type','version','master','description','last_update_time','create_time','host_list','api_list','suite_list']


class ApiRunRecordSerializer(serializers.ModelSerializer):
	'''API执行结果序列化'''
	api=ApiSerializer()
	class Meta:
		model=ApiRunRecord
		fields='__all__'

class RunApiPermodSerializer(serializers.ModelSerializer):
	'''性能测试结果序列化'''
	api=ApiSerializer()
	class Meta:
		model=RunApiPermod
		fields='__all__'

class SuiteApiRunRecordSerializer(serializers.ModelSerializer):
	api=ApiSerializer()
	class Meta:
		model=SuiteApiRunRecord
		fields='__all__'

class SuiteRunRecordSerializer(serializers.ModelSerializer):
	suite_records=SuiteApiRunRecordSerializer(many=True)
	suite=SuiteSerializer()
	class Meta:
		model=SuiteRunRecord
		fields='__all__'


